﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_ExtraTypes : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_HRM_PR_ExtraTypes ext = new B_HRM_PR_ExtraTypes();
        IBLL_HRM_PS_EmployeeTypes et = new B_HRM_PS_EmployeeTypes();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                DropDownList_Contact1.LoadControl(300, null);
                LoadDDLEmpTypeSeq();
                UpdateUI1("ViewAll");
            }
        }

        protected void LoadRadGrid1()
        {
            RadGrid1.DataSource = ext.GetFull(ddlEmpTypeSeq.SelectedValue, "D", null, null);
        }

        protected void LoadDDLEmpTypeSeq()
        {
            ddlEmpTypeSeq.DataSource = et.GetList();
            ddlEmpTypeSeq.DataBind();

            ddlEmpTypeSeq.SelectedIndex = 0;
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
            }
        }

        protected void ClearControl1()
        {
            lblExtSeq.Text = "";
            txtExtCode.Text = "";
            txtExtOthCode.Text = "";
            txtExtName.Text = "";
            txtExtNameShort.Text = "";
            rblExtTax.SelectedIndex = 0;
            rblExtMonthly.SelectedIndex = 0;
            ddlEmpTypeSeq.Enabled = true;
            txtExtCode.Enabled = true;
            txtExtOthCode.Enabled = true;
            txtExtName.Enabled = true;
            txtExtNameShort.Enabled = true;
            rblExtTax.Enabled = true;
            rblExtMonthly.Enabled = true;
            txtRemarks.Enabled = true;
            lblEditSystem.Visible = false;
        }

        //============================ control events ==============================

        //protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    if (e.CommandName == "RowClick" && e.Item is GridDataItem)
        //    {
        //        e.Item.Selected = true;
        //        string strReceiveTypeSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_type_seq"].Text;
        //        Session["ReceiveTypeSeq"] = strReceiveTypeSeq;
        //    }
        //}

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strExtSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_seq"].Text;
                string strExtCode = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_code"].Text;
                string strExtOthCode = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_oth_code"].Text;
                string strExtName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_name"].Text;
                string strExtNameShort = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_name_short"].Text;
                //string strExtFlag = "D";
                string strExtTax = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_tax"].Text;
                string strExtMonthly = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_monthly"].Text;
                string strRemarks = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["remarks"].Text;
                string strEditSystem = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["edit_system"].Text;
                string strContactSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["contact_seq"].Text;
                string strContactName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["contact_name"].Text;
                bool bCanEdit = main.WebString(strEditSystem) == "GFMS";

                lblExtSeq.Text = main.WebString(strExtSeq);
                txtExtCode.Text = main.WebString(strExtCode);
                txtExtOthCode.Text = main.WebString(strExtOthCode);
                txtExtName.Text = main.WebString(strExtName);
                txtExtNameShort.Text = main.WebString(strExtNameShort);
                rblExtTax.SelectedValue = main.WebString(strExtTax);
                rblExtMonthly.SelectedValue = main.WebString(strExtMonthly);
                txtRemarks.Text = main.WebString(strRemarks);
                DropDownList_Contact1.SetSelectedValue(main.WebString(strContactSeq));

                lblEditSystem.Visible = !bCanEdit;
                ddlEmpTypeSeq.Enabled = false;
                txtExtCode.Enabled = bCanEdit;
                txtExtOthCode.Enabled = bCanEdit;
                txtExtName.Enabled = bCanEdit;
                txtExtNameShort.Enabled = bCanEdit;
                rblExtTax.Enabled = bCanEdit;
                rblExtMonthly.Enabled = bCanEdit;
                txtRemarks.Enabled = bCanEdit;
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strExtSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ext_seq"].Text;

                info.ext_seq = Convert.ToInt32(strExtSeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = dtNow;

                ext.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
            //string strReceiveTypeSeq = lblReceiveTypeSeq.Text;
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;
            string strExtCode = txtExtCode.Text;
            string strExtOthCode = txtExtOthCode.Text;
            string strExtName = txtExtName.Text;
            string strExtNameShort = txtExtNameShort.Text;
            string strExtFlag = "D";
            string strExtTax = rblExtTax.SelectedValue;
            string strExtMonthly = rblExtMonthly.SelectedValue;
            string strRemarks = txtRemarks.Text;
            string strContactSeq = DropDownList_Contact1.GetSelectedValue();
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.emp_type_seq = main.ConvertNullInt(strEmpTypeSeq);
            info.ext_code = strExtCode;
            info.ext_oth_code = strExtOthCode;
            info.ext_name = strExtName;
            info.ext_name_short = strExtNameShort;
            info.ext_flag = strExtFlag;
            info.ext_tax = strExtTax;
            info.ext_monthly = strExtMonthly;
            info.remarks = strRemarks;
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            info.edit_system = "GFMS";
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            ext.Insert(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_HRM_PR_ExtraTypes info = new M_HRM_PR_ExtraTypes();
            string strExtSeq = lblExtSeq.Text;
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;
            string strExtCode = txtExtCode.Text;
            string strExtOthCode = txtExtOthCode.Text;
            string strExtName = txtExtName.Text;
            string strExtNameShort = txtExtNameShort.Text;
            string strExtFlag = "D";
            string strExtTax = rblExtTax.SelectedValue;
            string strExtMonthly = rblExtMonthly.SelectedValue;
            string strRemarks = txtRemarks.Text;
            string strContactSeq = DropDownList_Contact1.GetSelectedValue();
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.ext_seq = Convert.ToInt32(strExtSeq);
            info.emp_type_seq = main.ConvertNullInt(strEmpTypeSeq);
            info.ext_code = strExtCode;
            info.ext_oth_code = strExtOthCode;
            info.ext_name = strExtName;
            info.ext_name_short = strExtNameShort;
            info.ext_flag = strExtFlag;
            info.ext_tax = strExtTax;
            info.ext_monthly = strExtMonthly;
            info.remarks = strRemarks;
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            //info.edit_system = "GFMS";
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            ext.Update(info);

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        protected void ddlEmpTypeSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }
        
        //==========================================================================
    }
}
